#include "websocketserver.h"


WebSocketServer::WebSocketServer(QObject *parent) : QTcpServer(parent)
{

}


bool WebSocketServer::hasPendingConnections() const
{
    return v_pendingSocket.count() > 0;
}

WebSocket *WebSocketServer::nextPendingConnection()
{
    return v_pendingSocket.takeFirst();
}

void WebSocketServer::incomingConnection(qintptr descr)
{
    WebSocket *socket = new WebSocket();
    socket->setSocketDescriptor(descr);
    v_notConnectedSocket.append(socket);
    QObject::connect(socket, SIGNAL(handshakeSuccess()), this, SLOT(changeIntoPending()));
}

void WebSocketServer::changeIntoPending()
{
    WebSocket *socket = static_cast<WebSocket*>(sender());
    v_pendingSocket.append(socket);
    v_notConnectedSocket.removeAll(socket);
    Q_EMIT socketReady();
}
